﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Configuration;
using NFinal.Compile;

namespace WebCompiler
{
    class Program
    {
        public static void WebCompiler()
        {
            try
            {
                Console.WriteLine("生成开始");
                string appRoot = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName + "\\";
                //获取WebConfig中的配置信息
                Console.WriteLine("获取Web.Config中的配置信息");
                NFinal.Compile.Frame frame = new NFinal.Compile.Frame(appRoot);
                Console.WriteLine("获取所有的项目");
                string[] Apps = frame.GetApps();
                //创建主路由
                Console.WriteLine("获取路由重写信息");
                System.Collections.Generic.List<NFinal.Compile.ControllerFileData> fileDataList = frame.GetAllControllers();
                NFinal.Compile.ReWriteData rewriteData = NFinal.Compile.ConfigurationManager.GetReWriteData();
                rewriteData.AddReWriteData(NFinal.Compile.Frame.reWriteData);
                frame.CreateActionSearch(fileDataList, rewriteData);
                frame.CreateModuleAndRouter(fileDataList, rewriteData);
                frame.CreateOwinMiddlewareAndRouter(fileDataList, rewriteData);
                frame.CreateURLHelper(fileDataList, rewriteData);
                foreach (string app in Apps)
                {
                    //创建并生成Web站点
                    Console.WriteLine(app + ":开始生成");
                    NFinal.Compile.Config config = NFinal.Compile.ConfigurationManager.GetConfig(app, appRoot);
                    Console.WriteLine(app + ":获取当前配置");
                    NFinal.Compile.Application application = new NFinal.Compile.Application(config);
                    Console.WriteLine(app + ":获取数据库信息");
                    frame.GetDB(config);
                    Console.WriteLine(app + ":创建Application");
                    application.CreateApp();
                    Console.WriteLine(app + ":创建Models");
                    application.CreateModelsFile();
                    Console.WriteLine(app + ":创建cookieManager类");
                    application.CreateCookieManager();
                    application.CreateSessionManager();
                    Console.WriteLine(app + ":创建DAL");
                    application.CreateDAL(true);
                    Console.WriteLine(app + ":创建Compile");
                    application.CreateCompile(true);
                    frame.ClearDB();
                    Console.WriteLine(app + ":生成结束");
                }
                Console.WriteLine("生成结束");
            }
            catch (Exception e)
            {
                Console.WriteLine("编译失败，请检查代码。");
            }
        }

        static void Main(string[] args)
        {
            //编译文件
            WebCompiler();
        }
    }
}
